﻿using FirstLimitUpMode.Data;
using FirstLimitUpMode.ViewModels;
using GpCommon.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FirstLimitUpMode.Views
{
    /// <summary>
    /// GpDetailInfoView.xaml 的交互逻辑
    /// </summary>
    public partial class GpDetailInfoView : Window
    {
        private string m_WindowFile = "DataTxt//DetailViewInfo.txt";
        private KeyboardHook _keyboardHook;
        private GpDetailInfoVModel _vModel;

        public GpDetailInfoView(string code, float mark97, bool isTopBuy)
        {
            InitializeComponent();

            _vModel = new GpDetailInfoVModel(this, code, mark97, isTopBuy);
            this.DataContext = _vModel;

            _keyboardHook = new KeyboardHook();
            _keyboardHook.SetHook();
            _keyboardHook.SetOnKeyDownEvent(Win32_Keydown);
            _keyboardHook.SetOnKeyUpEvent(Win32_Keyup);
        }

        #region 全局键盘
        private Key _lastKey = Key.None;

        private void Win32_Keydown(Key key)
        {
            if (key == Key.LeftAlt)
            {
                _lastKey = key;
            }
            else if (_lastKey == Key.LeftAlt && key == Key.A)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                }
            }
            else if(key == Key.Escape)
            {
                this.Close();
            }
            else
            {
                _lastKey = Key.None;
            }
        }
        private void Win32_Keyup(Key key)
        {
            if (key == Key.LeftAlt)
            {
                _lastKey = Key.None;
            }
        }



        #endregion

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _keyboardHook.UnHook();
            WriteWindowFile();
            _vModel._view = null;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ReadWindowFile();
        }

        private void ReadWindowFile()
        {
            try
            {
                if (File.Exists(m_WindowFile))
                {
                    string str = File.ReadAllText(m_WindowFile);
                    var strs = str.Split(':');
                    if(strs.Count() == 2)
                    {
                        this.Top = double.Parse(strs[0]);
                        this.Left = double.Parse(strs[1]);
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        private void WriteWindowFile()
        {
            try
            {
                string msg = this.Top.ToString() + ":" + this.Left.ToString();
                File.WriteAllText(m_WindowFile, msg);
            }
            catch (Exception)
            {

            }
        }
    }
}
